package com.jushu.storbox;

import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.jushu.storbox.actions.AlbumContext;
import com.jushu.storbox.adapter.AlbumCatchSettingAdapter;
import com.jushu.storbox.entities.AlbumEntity;
import com.jushu.storbox.manager.ActivityTaskManager;
import com.jushu.storbox.util.LogUtil;

/**
 * 
 */
public class SetAlbumBackupActivity extends Activity implements OnClickListener, OnItemClickListener {

	private final static String LOG_TAG = "SetAlbumBackupActivity";
	private final static int START_REQUEST_ALBUM = 0;
	private final static int STOP_REQUEST_ALBUM = 1;

	private AlbumCatchSettingAdapter adapter;

	private GridView mGroupGridView;

	private View mBtnBack;
	private Button album_setting_close;
	private Button album_setting_confirm;
	private LinearLayout loadding_wait_layout;

	private AlbumContext albumContext = null;

	private Handler uiHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case START_REQUEST_ALBUM:
				if (loadding_wait_layout != null) {
					loadding_wait_layout.setVisibility(View.VISIBLE);
				}
				break;

			case STOP_REQUEST_ALBUM:
				if (loadding_wait_layout != null) {
					loadding_wait_layout.setVisibility(View.GONE);
				}
				break;

			default:
				super.handleMessage(msg);
			}
		}
	};

	class SetAdapterAsyncTask extends AsyncTask<Void, Void, List<AlbumEntity>> {
		@Override
		protected List<AlbumEntity> doInBackground(Void... arg0) {
			uiHandler.obtainMessage(START_REQUEST_ALBUM).sendToTarget();
			List<AlbumEntity> list = albumContext.getAlbumList(SetAlbumBackupActivity.this);
			return list;
		}

		@Override
		protected void onPostExecute(List<AlbumEntity> result) {
			adapter = new AlbumCatchSettingAdapter(SetAlbumBackupActivity.this, result);
			mGroupGridView.setAdapter(adapter);
			uiHandler.obtainMessage(STOP_REQUEST_ALBUM).sendToTarget();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityTaskManager.getInstance().putActivity(LOG_TAG, SetAlbumBackupActivity.this);
		LogUtil.i(LOG_TAG, "onCreate()");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_setting_album);
		findViews();
		setLiteners();
		albumContext = new AlbumContext(SetAlbumBackupActivity.this);
		new SetAdapterAsyncTask().execute();
	}

	private void findViews() {
		mGroupGridView = (GridView) findViewById(R.id.album_setting_folders);
		mBtnBack = findViewById(R.id.layout_back);
		album_setting_close = (Button) findViewById(R.id.album_setting_close);
		album_setting_confirm = (Button) findViewById(R.id.album_setting_confirm);
		loadding_wait_layout = (LinearLayout) findViewById(R.id.loadding_wait_layout);
	}

	private void setLiteners() {
		mGroupGridView.setOnItemClickListener(this);
		mBtnBack.setOnClickListener(this);
		album_setting_close.setOnClickListener(this);
		album_setting_confirm.setOnClickListener(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		LogUtil.i(LOG_TAG, "onCreate()");
		ActivityTaskManager.getInstance().removeActivity(LOG_TAG);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_back:
			onBackPressed();
			break;

		case R.id.album_setting_close:
			onBackPressed();
			break;

		case R.id.album_setting_confirm:
			onSettingConfirm();
			onBackPressed();
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	}

	private void onSettingConfirm() {
		albumContext.saveAlbumEntity(SetAlbumBackupActivity.this);
	}
}
